package com.dhnsoft.代码随想录.栈与队列;

import java.util.Stack;

/**
 * ClassName: d03_20_有效的括号
 * Package: com.dhnsoft.代码随想录.栈与队列
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/26 10:03
 * @Version 1.0
 */
public class d03_20_有效的括号 {

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();
        char[] chars = s.toCharArray();
        if(chars.length==1||chars.length==0){
            return false;
        }
        for (int i=0;i<chars.length;i++) {
            if(!stack.isEmpty()){
                if((chars[i]=='}'&&stack.peek()=='{')||(chars[i]==']'&&stack.peek()=='[')||(chars[i]==')'&&stack.peek()=='(')){
                    stack.pop();
                    continue;
                }
            }
            stack.push(chars[i]);
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        boolean valid = isValid("){");
        System.out.println(valid);
    }
}
